readme='''
########################################################################
#目的:本程序的目的是为了验证邮寄地址的有效性

#原理:把地址交由腾讯的开放web api验证有效性，通过反馈的结果做判断是否有效
#腾讯接口http://apis.map.qq.com/ws/geocoder/v1/?key=ZYIBZ-NNOWF-GA4JK-N5IFR-QHHWV-SDFEG&address=宁夏自治区银川市兴庆区友爱南街
#通过腾讯的反馈结果中的'reliability'可靠度 小于4为不可靠，且'level'层级小于4，同时满足，则结果不可靠
#免责声明:本代码也不能保证100%准确， 只能说明该地址在腾讯地图里能否找到， 如果未收录高德地图， 或者 某某路交叉口 这样的地址 是无法做出判断的， 或者说系统会判断是错误或存疑地址。
#设计人员: jianbo.wang
#日期:2021.5.22
#代码类别:python 测试环境:3.7.2  外网机， 如将来内网中运行，需要添加代理
#版本:1.3
#实测速度每秒10条

#########################################################################
'''

#第一部分 导入所需的各类函数
import time
import requests
import time
import json
import os
import logging
import datetime
import chardet
#腾讯web api 输入地址， 通过网页抓取， 返回腾讯地图确认存在的地址， 方便后续做比对
def qq_web_api(address,key):
    requests.packages.urllib3.disable_warnings()
    try:
        response = requests.get('http://apis.map.qq.com/ws/geocoder/v1/?key={key}&address={address}'.format(key=key,address=address), timeout=5, verify=False)
#        print('response',response)
        text=response.text
#        print('text:',text)
        #a=input('hello')

        data = json.loads(text)
    except:
        data=demo
        print('貌似网络有问题')
        logger.info('貌似网络有问题')
        time.sleep(20)

    return data

demo={
        "status": 0,
        "message": "query ok",
        "result": {
            "title": "金水区路劲大厦",
            "location": {
                "lng": 113.721397,
                "lat": 34.777721
            },
            "ad_info": {
                "adcode": "410105"
            },
            "address_components": {
                "province": "河南省",
                "city": "郑州市",
                "district": "金水区",
                "street": "商务外环路",
                "street_number": "14"
            },
            "similarity": 0.8,
            "deviation": 1000,
            "reliability": 7,
            "level": 10
        }
    }

#按顺序给出keys列表中的一个
def qq_map_keys():
    global keys_i
    
    #print(keys_i)
    data=keys
    len_data=len(data)
    i=keys_i%len_data
    key=list(data.keys())[i]
    #print(key,data[key])
    #print('len',len(data))
    keys_i=keys_i+1
    return data[key]

#读取json文件中的数据
def read_key_from_file(file):
    with open(file,'r',encoding='utf-8') as file:
        x=json.load(file)
    return x

def write_json(data,file):
    with open(file,'w',encoding='utf-8') as f:
        f.write(json.dumps(data,ensure_ascii=False,indent=4))
def txt_file():
    for file in os.listdir('../input/'):
        if file.endswith('.txt'):
            return '../input/'+file


#逐行处理输入的txt，处理完成后，输出json到output,并删除任务txt文件
def actions(file):
    starttime=time.time()
    today=str(datetime.date.today())
    global handler
    handler = logging.FileHandler("../logs/"+today+".log")
    global logger
    logger = logging.getLogger(__name__)
    logger.setLevel(level = logging.INFO)
    #handler = logging.FileHandler("../logs/log.txt")
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
     
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
     
    logger.addHandler(handler)
    logger.addHandler(console)
    logger.info('file:'+file)
    #逐行读取任务txt
    with open(file,mode='rb') as f3:
        content = f3.read()
    code = chardet.detect(content)['encoding']
    with open (file,'r',encoding=code) as f:
    #with open (file,'r') as f:
        data=f.readlines()



    print('**************************************')
    dict0={}
    results={}
    for line in data:
        #print(line)
        ser,addr=line.split(',')
        dict0[ser]=addr.replace(' ','').replace('\n','')

    
    #print(dict0)
    #write_json(dict0,'1.json')
    i=0
    dict_failed={}
    for item in dict0:
        i=i+1
        if i%20==0:
            print(i)
        key=qq_map_keys()
        result=qq_web_api(dict0[item],key)
        addr=dict0[item]
        dict0[item]={}
        try:
        
            dict0[item]['reliability']=result['result']['reliability']
            dict0[item]['level']=result['result']['level']
            dict0[item]['addr']=addr
            dict0[item]['result']=result
        except:
            result=demo
            dict0[item]['reliability']=result['result']['reliability']
            dict0[item]['level']=result['result']['level']
            dict0[item]['addr']=addr
            dict0[item]['result']=result
        #try:
        if result['result']['level']<4 and result['result']['reliability']<4:
            dict_failed[item]={}
            dict_failed[item]['reliability']=result['result']['reliability']
            dict_failed[item]['level']=result['result']['level']
            dict_failed[item]['addr']=addr
            dict_failed[item]['result']=result
            print('item',item,dict0[item],result['result']['level'],result['result']['reliability'])
    filename=os.path.basename(file)
    write_json(dict_failed,'../output/'+filename+'_failed.json')
    write_json(dict0,'../output/'+filename+'_results.json')
    print('*************end')
    os.remove(file)
    endtime=time.time()
    logger.info('file结束:'+file+"条目数:"+str(i)+" 错误条目数:"+str(len(dict_failed))+' 耗时(秒)'+str(endtime-starttime))
        
    
        
if __name__ =='__main__':
    #导入腾旭地图的api key列表， 每个key每天只能调用1万次，我们做一个keys组合, 扩展调用的次数， 例如放10个key, 每日最大调用就能到10万次
    keys=read_key_from_file('qq_map_keys.json')
    #初始化调用次数
    keys_i=0
    #日志的配置

    #计划任务，每10秒轮询，看是否有txt在输入文件夹中，处理完成后，输出json到output,并删除任务txt文件
    while True:
        
        if txt_file():
            #logger.info('开始处理文件:'+txt_file())
            print('开始处理文件:',txt_file())
            #with open (txt_file(),'r',encoding='utf-8') as f:
            actions(txt_file())
        else:
            print('等待输入新的任务txt文件 ......')
        time.sleep(10)